<?php


class Api_buy_mdl extends CI_Model
{
	
	const TBL_LOTTERY = 'cj_lottery';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	
	/**
	 * ids
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function outputdata($data)
	{
		header('Content-Type:text/html; charset=utf-8', false);
		$lotteryn = array();
		
		if(isset($data['BUYNUMS']) && !empty($data['BUYNUMS']) && !empty($data['DRAWNO']))
		{
			$dataids = explode(',', $data['BUYNUMS']);
			$lotteryn = $this->db->select('lottery.*,reward.singleprice,product.*')
						->where('reward.enabled',1)
						->where('lottery.rewardid',$data['DRAWNO'])
						->where_in('lottery.rnum',$dataids)
						->from(self::TBL_LOTTERY.' as lottery')
						->join('cj_reward as reward','lottery.rewardid = reward.id')
						->join('cj_products as product','product.id = lottery.productid','left outer')
						->get()
						->result_array();
		}
		
		$buyids = array();
		$nobuyids = array();
		$product = array();
		$total = 0;
		
		foreach($lotteryn as $v)
		{
			if(empty($v['uid']))
			{
				$buyids[] = $v['rnum'];
				$total+=$v['singleprice'];
				
				if(!empty($v['productid']))
				{
					$product[$v['rnum']] = array($v['productid'],$v['ptype'],$v['productname'],$v['content']);
				}
			}
			else
			{
				$nobuyids[] = $v['rnum'];
			}
		}
		
		$goal = $this->getusergoal($data['USERID']);
		$usergoal = $goal['txgoldnum'];
		
		if($usergoal >=$total)
		{
			//购买成功
			$this->buy($data['USERID'], $data['DRAWNO'],$buyids);
			$tmp['TAG'] = $data['TAG'];
			$tmp['REWARED'] = !empty($buyids) ? implode(',', $buyids) : null;
			$tmp['NOREWARD'] = !empty($nobuyids) ? implode(',', $nobuyids) : null;
			$tmp['BUYED'] = !empty($nobuyids) ? implode(',', $nobuyids) : null;
			$tmp['GOLAL'] = $usergoal - $total;
			$tmp['TOTAL'] = $total;
			$tmp['ENABLED'] = TRUE;
			$tmp['PRODUCT'] = $product;
			$tmp['MESSAGE'] = array();
			//扣除金币
			$this->changegoal($data['USERID'],$usergoal - $total);
			//检查活动状态
			$this->checkrewardstatic($data['DRAWNO']);
			
		}
		else 
		{
			$tmp['TAG'] = $data['TAG'];
			$tmp['REWARED'] =  null;
			$tmp['NOREWARD'] =  null;
			$tmp['BUYED'] = null;
			$tmp['GOLAL'] = $goal;
			$tmp['TOTAL'] = $total;
			$tmp['ENABLED'] = FALSE;
			$tmp['PRODUCT'] = NULL;
			$tmp['MESSAGE'] = array('金币不足!');
		}		
		

		echo json_encode($tmp);
	}
	
	
	private function buy($uid,$rewardid,$drawNo)
	{
		$result = 0;
		if(!empty($drawNo))
		{
			$this->db->where('rewardid',$rewardid);
			$this->db->where_in('rnum', $drawNo);
			$result = $this->db->update(self::TBL_LOTTERY, array('uid'=>$uid,'createtime'=>time()));	
		}
		return $result;
	}
	
	/**
	 * 
	 * 用户金币数量 ...
	 * @param unknown_type $uid
	 */
	private function getusergoal($uid)
	{
		$data = array();
		$this->db->select('user.uid,user.txusername,user.txgoldnum')->from('cj_users as user')->where('user.uid', $uid)->limit(1);
		$query = $this->db->get();
		
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
		
	}
	
	/**
	 * 扣除用户金币
	 */
	
	private function changegoal($userid,$goal)
	{
		$data['txgoldnum'] = intval($goal);
		$this->db->where('uid', intval($userid));
		$this->db->update('cj_users', $data);
		
		return $this->db->affected_rows();
	}
	
	
	/**
	 * 检查活动状态
	 */
	
	private function checkrewardstatic($rewardid)
	{
		$lottery = $this->db->select('lottery.id,lottery.uid,lottery.rewardid,lottery.rnum')
						->where('lottery.rewardid',$rewardid)
						->from(self::TBL_LOTTERY.' as lottery')
						->get()->result_array();
						
		//未被购买的号码
		if(!empty($lottery))
		{
			foreach($lottery as $v)
			{
				if(empty($v['uid']))
				{
					$nobuy[] = $v['rnum'];
				}
			}
		}
		
		if(empty($nobuy))
		{
			$data['enabled'] = 2;
			$this->db->where('id', intval($rewardid));
			$this->db->update('cj_reward', $data);
		}		
	}
}